package com.aaa.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

// 统一响应结果
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    private Integer code; // 业务状态码
    private String message; // 提示信息
    private T data; // 响应数据

    // 成功状态码
    public static final int SUCCESS = 200;
    // 失败状态码（通用）
    public static final int FAIL = 500;
    // 参数错误
    public static final int PARAM_ERROR = 400;
    // 未授权
    public static final int UNAUTHORIZED = 401;
    // 禁止访问
    public static final int FORBIDDEN = 403;
    // 资源不存在
    public static final int NOT_FOUND = 404;
    // 服务器内部错误
    public static final int INTERNAL_ERROR = 500;
    // 业务错误
    public static final int BUSINESS_ERROR = 501;
    // 重复操作
    public static final int DUPLICATE_ERROR = 502;
    // 验证失败
    public static final int VALIDATION_ERROR = 503;

    // 快速返回操作成功响应结果（带响应数据）
    public static <E> Result<E> success(E data) {
        return new Result<>(SUCCESS, "操作成功", data);
    }

    // 快速返回操作成功响应结果
    public static Result success() {
        return new Result(SUCCESS, "操作成功", null);
    }

    // 参数错误
    public static Result paramError(String message) {
        return new Result(PARAM_ERROR, message, null);
    }

    public static Result paramError(String message, String detail) {
        return new Result(PARAM_ERROR, message + (detail != null ? ": " + detail : ""), null);
    }

    // 未授权
    public static Result unauthorized(String message) {
        return new Result(UNAUTHORIZED, message, null);
    }

    // 禁止访问
    public static Result forbidden(String message) {
        return new Result(FORBIDDEN, message, null);
    }

    // 资源不存在
    public static Result notFound(String message) {
        return new Result(NOT_FOUND, message, null);
    }

    // 服务器内部错误
    public static Result internalError(String message) {
        return new Result(INTERNAL_ERROR, message, null);
    }

    // 业务错误
    public static Result businessError(String message) {
        return new Result(BUSINESS_ERROR, message, null);
    }

    // 重复操作
    public static Result duplicateError(String message) {
        return new Result(DUPLICATE_ERROR, message, null);
    }

    // 验证失败
    public static Result validationError(String message) {
        return new Result(VALIDATION_ERROR, message, null);
    }

    // 通用错误（向后兼容）
    public static Result error(String message) {
        return new Result(FAIL, message, null);
    }

    public static Result error(String message, String detail) {
        return new Result(FAIL, message + (detail != null ? ": " + detail : ""), null);
    }

    // 支持自定义状态码
    public static <E> Result<E> custom(int code, String message, E data) {
        return new Result<>(code, message, data);
    }
}
